# -*- coding: utf-8 -*-
# @Time    : 2018-05-21 19:28
# @Author  : Yang Feng
# @Email   : yangfeng@bbdservice.com
# @File    : AvgRepaymentPercent_M6.py
# @Software: PyCharm

'''eof
name:最近6个月平均应还款占比
code:AvgRepaymentPercent_M6
tableName:
columnName:
groups:个人征信
dependencies:IC
type:常用指标
datasourceType:在线指标
description:
eof'''


import sys
import json
reload(sys)
sys.setdefaultencoding('utf-8')


def FormatData(tableName):
    try:
        icr = json.loads(IC[0]["icr"])[tableName]
        return icr if isinstance(icr, list) and len(icr) > 0 else [{}]
    except:
        return [{}]


def ConvertDataType(dataValue, dataType):
    returnData = None
    if dataType == 'float':
        try:
            returnData = float(dataValue) if float(dataValue) >= 0 else None
        except:
            returnData = None
    elif dataType == 'None':
        returnData = 0 if dataValue is None else dataValue
    return returnData


def AvgRepaymentPercentM6():
    try:
        # 未结清贷款信息汇总
        icrUnpaidLoan = FormatData("ICRUnpaidLoan")
        # 未销户贷记卡信息汇总
        icrUndestoryLoancard = FormatData("ICRUndestoryLoancard")
        # 未销户准贷记卡信息汇总
        icrUndestoryStandardLoancard = FormatData("ICRUndestoryStandardLoancard")
        if icrUnpaidLoan == [{}] and icrUndestoryLoancard == [{}] and icrUndestoryStandardLoancard == [{}]:
            return u"缺失值"
        else:
            # 最近6个月平均应还款
            latest6MonthUsedAvgAmountUl = ConvertDataType(icrUnpaidLoan[0].get("Latest6MonthUsedAvgAmount"), 'float')
            # 授信总额
            creditLimitUl = ConvertDataType(icrUnpaidLoan[0].get("CreditLimit"), 'float')
            latest6MonthUsedAvgAmountUl = None if creditLimitUl is None else latest6MonthUsedAvgAmountUl
            creditLimitUl = None if latest6MonthUsedAvgAmountUl is None else creditLimitUl

            # 最近6个月平均使用额度
            latest6MonthUsedAvgAmountUlc = ConvertDataType(icrUndestoryLoancard[0].get("Latest6MonthUsedAvgAmount"), 'float')
            # 授信总额
            creditLimitUlc = ConvertDataType(icrUndestoryLoancard[0].get("CreditLimit"), 'float')
            latest6MonthUsedAvgAmountUlc = None if creditLimitUlc is None else latest6MonthUsedAvgAmountUlc
            creditLimitUlc = None if latest6MonthUsedAvgAmountUlc is None else creditLimitUlc

            # 最近6个月平均使用额度（未销户）
            latest6MonthUsedAvgAmountUslc = ConvertDataType(icrUndestoryStandardLoancard[0].get("Latest6MonthUsedAvgAmount"), 'float')
            # 授信总额
            creditLimitUslc = ConvertDataType(icrUndestoryStandardLoancard[0].get("CreditLimit"), 'float')
            latest6MonthUsedAvgAmountUslc = None if creditLimitUslc is None else latest6MonthUsedAvgAmountUslc
            creditLimitUslc = None if latest6MonthUsedAvgAmountUslc is None else creditLimitUslc

            if latest6MonthUsedAvgAmountUl is None and latest6MonthUsedAvgAmountUlc is None and latest6MonthUsedAvgAmountUslc is None:
                return u"缺失值"
            if creditLimitUl is None and creditLimitUlc is None and creditLimitUslc is None:
                return u"缺失值"

            latest6MonthUsedAvgAmount = ConvertDataType(latest6MonthUsedAvgAmountUl, 'None')\
                                        +ConvertDataType(latest6MonthUsedAvgAmountUlc, 'None')\
                                        +ConvertDataType(latest6MonthUsedAvgAmountUslc, 'None')
            creditLimit = ConvertDataType(creditLimitUl, 'None')+ConvertDataType(creditLimitUlc, 'None')\
                          +ConvertDataType(creditLimitUslc, 'None')
            if creditLimit == 0:
                return u"缺失值"
            return round(latest6MonthUsedAvgAmount/creditLimit, 4)
    except:
        return u"缺失值"


result = AvgRepaymentPercentM6()